var app = getApp();
// pages/demo03/demo03.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ID:0,
        hasUserInfo:false,
        token:"",
        Title:[
            {
                id:0,
                name:"资讯",
                isActive:true
            },
            {
                id:1,
                name:"实习",
                isActive:false
            },
            {
                id:2,
                name:"考研",
                isActive:false
            },
    
        ],

        ImageList:[
            {
                id:0,
                url:"https://thirdwx.qlogo.cn/mmopen/vi_32/uJ7wdTmBn86zpPqyRd4cE4ia0yMfaR7peBFslEibjOmTiaKE28dkKwVOQXLdUgDg9cAQ7Lho2fP0QxYpuibeGzSsvw/132",
                title:"广东某洋大学生每晚偷学C++ 毕业后年薪高达百W"
            },

            {
                id:1,
                url:"https://thirdwx.qlogo.cn/mmopen/vi_32/uJ7wdTmBn86zpPqyRd4cE4ia0yMfaR7peBFslEibjOmTiaKE28dkKwVOQXLdUgDg9cAQ7Lho2fP0QxYpuibeGzSsvw/132",
                title:"震惊：学会这几招一年省下一百W！！！！"
            },
            {
                id:2,
                url:"https://thirdwx.qlogo.cn/mmopen/vi_32/uJ7wdTmBn86zpPqyRd4cE4ia0yMfaR7peBFslEibjOmTiaKE28dkKwVOQXLdUgDg9cAQ7Lho2fP0QxYpuibeGzSsvw/132",
                title:"一男子吃霸王餐老板很开心，背后原因让人暖心"
            },
            {
                id:3,
                url:"https://thirdwx.qlogo.cn/mmopen/vi_32/uJ7wdTmBn86zpPqyRd4cE4ia0yMfaR7peBFslEibjOmTiaKE28dkKwVOQXLdUgDg9cAQ7Lho2fP0QxYpuibeGzSsvw/132",
                title:"震惊：这几样食物吃错了堪比毒药！！！！"
            },
            {
                id:4,
                url:"https://thirdwx.qlogo.cn/mmopen/vi_32/uJ7wdTmBn86zpPqyRd4cE4ia0yMfaR7peBFslEibjOmTiaKE28dkKwVOQXLdUgDg9cAQ7Lho2fP0QxYpuibeGzSsvw/132",
                title:"震惊：吃这几样食品就是在慢性自杀！！！！"
            }


        ],



    },




    //检测点击哪个标题改变显示内容和标题状态
    handItemchange(e){
        const {index}=e.currentTarget.dataset;
        let {Title}=this.data;
        Title.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
        this.setData({
            Title
        })
    },


    
    //跳转到文章页面并传递参数
    navigato(e){
        wx.navigateTo({
          url: '/pages/demo01/demo01?current='+ JSON.stringify(e.currentTarget.id),
        })
    }, 

    onLoad(){
        console.log(wx.getStorageSync('token'))
        this.setData({
            hasUserInfo:wx.getStorageSync('hasUserInfo')
        })
    },

    //获取用户信息
    getUserProfile(e) {


    
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
          desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
              var nickName=res.userInfo.nickName
              var url=res.userInfo.avatarUrl
            console.log(res)
            this.setData({
                hasUserInfo:true
            })
            wx.setStorageSync("UserAvatarUrl",res.userInfo.avatarUrl)
            wx.setStorageSync("UserNickName",res.userInfo.nickName)
            wx.setStorageSync('hasUserInfo',true)
        console.log("缓存用户数据成功")

        wx.login({
            success(res){
                console.log(res.code)
              if(res.code){
                wx.setStorage({
                  key: 'code',
                  data: res.code,
                })
  
                wx.request({
                  url: 'http://119.23.252.15:10001/loginUpdate',
                  data:{
                      "code":res.code,
                      "nickName":nickName,
                      "avatarUrl":url
  
                  },
                  method:"POST",
                  success(res){
                      wx.setStorage({
                        data: res.data.data.token,
                        key: "token",
                      success(){
                          console.log("设置用户token成功"+wx.getStorageSync('token'))
                          app.onLaunch()
                          }})
                  }
                })
              }
            }
          })
      },
    
                        })
                        },
})